<html>
<body>

<?php

$body = '';

// From simplecartjs
if (isset($_POST['itemCount'])) {
	
	$content = $_POST;
	for($i=1; $i < $content['itemCount'] + 1; $i++) {
		  $body .= '<tr>';
		  $name = 'item_name_'.$i;
		  $quantity =  'item_quantity_'.$i;
		  $price = 'item_price_'.$i;
		  $body .= '<td>'.$i.'</td><td>'.$content[$name].'</td><td>'.$content[$quantity].'</td><td>'.$content[$price].'</td>';
		  $body .= '</tr>';
	}
}


if (isset($_REQUEST['body'])) {
	$body = $_REQUEST['body'];
}

if ($_REQUEST['customer'] != '' and $_REQUEST['address'] != '') {
	$customer = $_REQUEST['customer'] ;
	$address = $_REQUEST['address'] ;
	$contact = $_REQUEST['contact'] ;
	
	$orderId = insertIntoOrders($customer, $address, $contact, $body);	

	echo "<h3>Order successfully submitted.</h3>";
	echo "<br/>";
	echo "Your order tracking number is: " . $orderId;
	echo "<br/>";
	echo "Thank you for ordering in Spira Shop!";
	echo "<br/>";
	//echo "<a href='http://spira.byethost4.com/index.html?ordercomplete'>Go back to spira.byethost4.com<a/>";
	echo "<a href='http://spira.byethost4.com'>Go back to spira.byethost4.com<a/>";
	//echo "<br/>";
	//echo "<br/>";
	//echo "<br/>";
	//echo $customer."<br/>".$address."<br/>".$body;
}
else {
  echo "
  <h3>Please complete up the form below & click submit</h3>
  <form method='post' action='sendcart.php'>
  Name: <input name='customer' type='text'><br>
  Address: <input name='address' type='text'><br>
  Contact #: <input name='contact' type='text'><br>
  Items:<br>
<table border='1'>
	<tr>
		<th>#</th>
		<th>Item</th>
		<th>Quantity</th>
		<th>Price</th>
	</tr>
	".$body."
</table> 

  <br>


  <input type='hidden' name='body' value='".$body."'/>
  <input type='submit'>
  </form>";
}

function insertIntoOrders($customer, $address, $contact, $body) {  
//,"b4_12694311_spira"
	$con=mysql_connect("sql300.byethost4.com","b4_12694311","bt121289");

	// Check connection
	if (!$con) {
    		die('Could not connect: ' . mysql_error() . "<a href='http://spira.byethost4.com/'>Go back to spira.byethost4.com<a/>");
	}

	mysql_select_db('b4_12694311_spira',$con) or die ("could not open db".mysql_error(). "<a href='http://spira.byethost4.com/'>Go back to spira.byethost4.com<a/>");

	mysql_query("INSERT INTO Orders (Customer, Address, Contact, Details)
					VALUES ('".$customer."', '".$address."', '".$contact."', '".$body."')", $con);
	$id = mysql_insert_id();
	
	mysql_close($con);  
	return $id;
}

?>

</body>
</html>

